package cn.ruituo.web;

import cn.ruituo.service.GradeService;
import cn.ruituo.util.LogUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

@WebServlet({"/DoAddStudentServlet","/toAddStudent"})
@MultipartConfig(maxFileSize = 50000000)//允许文件上传
public class DoAddStudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //直接拿值
        String sname=request.getParameter("sname");
        LogUtil.debug(sname+">>>>>>>>>>>");
        Part idcardpic = request.getPart("idcardpic");
        //idcardpic.getName():获得文件元素名
        String fileName=idcardpic.getSubmittedFileName();
        LogUtil.debug("文件名："+fileName);
        String path=request.getServletContext().getRealPath("images");

        idcardpic.write(path+"/"+fileName);
        LogUtil.debug(sname+"的身份证保存成功！"+path+"/"+fileName);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("grades", GradeService.service.findALL());
        request.getRequestDispatcher("/WEB-INF/views/addStudent.jsp").forward(request,response);
    }
}
